package com.javabasic.serialize.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.javabasic.serialize.model.User;

import java.io.IOException;

/**
 * @Author: mir_xiong
 * @Title: UserSerializer
 * @ProjectName: javabasic
 * @Description:
 * @Date: 2022/4/2 17:45
 */
public class UserSerializer extends StdSerializer<User> {

    public UserSerializer() {
        super(User.class);
    }

    @Override
    public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("userName", user.getUserName());
        jsonGenerator.writeStringField("Class", User.class.getSimpleName());
        jsonGenerator.writeNumberField("age", user.getAge());
        jsonGenerator.writeStringField("Sex", user.getSex().name());
        jsonGenerator.writeEndObject();
    }
}
